// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers;

[NonValidation]
//[AuthorizePolicy]
[AllowAnonymous]
//[MaMenu(code: "DataConfig", name: "配置", area: "HtAdmin",pCode :"SystemManager")]
// [Route("Dc")]
//[Route("[area]/Abc/[action]")]
[MaMvc("HtAdmin", controllerDescription: "数据配置", PCode = "HtAdmin_Developer")]
public class DataConfigController : HtAdminBaseController
{
    private readonly IDataConfigService _dataConfigService;
    private readonly ILogger<DataConfigController> _logger;

    public DataConfigController(ILogger<DataConfigController> logger
        , IDataConfigService dataConfigService)
    {
        _logger = logger;
        _dataConfigService = dataConfigService;
    }

    [HttpGet]
    [MaMenu("数据配置")]
    [MaPermission("浏览", "Index,GetList")]
    public async Task<IActionResult> Index(int pid = 0)
    {
        return View();
    }

    [UnifyResult(typeof(DataConfigOut))]
    [HttpGet]
    [HttpPost]
    public async Task<JsonResult> GetList(DataConfigQuery searchquery)
    {
        searchquery.Limit = 9999;
        var pagedlist = await _dataConfigService.GetAllByPageToLayui(searchquery);
        return Json(pagedlist);
    }

    [MaPermission("详情", "Info")]
    public async Task<ActionResult> Info(int? id, int dpid = 0)
    {
        ViewData["Dpid"] = dpid;
        DataConfigOut entity = null;
        if (id.HasValue) entity = await _dataConfigService.Get<DataConfigOut>(id.Value);
        return View(entity ?? new DataConfigOut());
    }

    [HttpPost]
    // [ValidateInput(false)]
    [MaPermission("编辑", "Edit")]
    public async Task<JsonResult> Edit(DataConfigInput input)
    {
        await _dataConfigService.InsertOrUpdate(input);
        return Json("");
    }

    [HttpPost]
    [MaPermission("删除", "Delete")]
    public async Task<JsonResult> Delete(int id)
    {
        await _dataConfigService.Delete(id);
        return Json("");
    }

    [HttpPost]
    [MaPermission("设置状态", "SetState")]
    public async Task<JsonResult> SetState(int id, bool state, string type)
    {
        await _dataConfigService.SetState(id, state, type);
        return Json("");
    }

    [MaPermission("获取数据配置树", "GetDcTrees")]
    [UnifyResult(typeof(DataConfigOut))]
    public async Task<JsonResult> GetDcTrees()
    {
        return Json(await _dataConfigService.GetDtree());
    }
}